package php4dvdPages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import php4dvdTests.Movie;
import php4dvdTests.MyPageFactory;

/**
 * @author Anna Alexeyenko
 *         Date: 1/26/13
 */
public class AddMoviePage extends InternalPage {


    @FindBy(id = "imdbsearch")
    private WebElement imdbSearchField;

    @FindBy(name = "imdbid")
    private WebElement imdbIdField;

    @FindBy(name = "name")
    private WebElement titleField;

    @FindBy(name = "aka")
    private WebElement alsoKnownAsField;

    @FindBy(name = "year")
    private WebElement yearField;

    @FindBy(name = "duration")
    private WebElement durationField;

    @FindBy(name = "rating")
    private WebElement ratingField;

    @FindBy(id = "formats")
    private WebElement formatField;

    @FindBy(id = "own_yes")
    private WebElement ownThisMovieField;

    @FindBy(id = "own_no")
    private WebElement notOwnThisMovieField;

    @FindBy(id = "seen_yes")
    private WebElement seenThisMovieField;

    @FindBy(id = "seen_no")
    private WebElement notSeenThisMovieField;

    @FindBy(id = "loaned_yes")
    private WebElement loanedThisMovieField;

    @FindBy(id = "loaned_no")
    private WebElement notLoanedThisMovieField;

    @FindBy(id = "cover")
    private WebElement coverField;

    @FindBy(name = "trailer")
    private WebElement trailerUrlField;

    @FindBy(name = "notes")
    private WebElement personalNotesField;

    @FindBy(name = "taglines")
    private WebElement taglinesField;

    @FindBy(name = "plotoutline")
    private WebElement plotOutlineField;

    @FindBy(name = "plots")
    private WebElement plotsField;

    @FindBy(name = "languages")
    private WebElement languagesField;

    @FindBy(name = "subtitles")
    private WebElement subtitlesField;

    @FindBy(name = "audio")
    private WebElement audioField;

    @FindBy(name = "video")
    private WebElement videoField;

    @FindBy(name = "country")
    private WebElement countryField;

    @FindBy(name = "genres")
    private WebElement genresField;

    @FindBy(name = "director")
    private WebElement directorField;

    @FindBy(name = "writer")
    private WebElement writerField;

    @FindBy(name = "producer")
    private WebElement producerField;

    @FindBy(name = "music")
    private WebElement musicField;

    @FindBy(name = "cast")
    private WebElement castField;

    @FindBy(name = "submit")
    private WebElement saveButton;

    @FindBy(name = "loanname")
    private WebElement loanName;

    @FindBy(name = "loandate")
    private WebElement loanDate;


    public boolean isOnThisPage() {
        return imdbSearchField.isDisplayed();
    }

    public MoviePage addNewMovie(Movie newMovie) {
        if (newMovie.getImdbIdField() != null) {
            imdbIdField.sendKeys(newMovie.getImdbIdField());
        }

        if (newMovie.getTitleField() != null) {
            titleField.sendKeys(newMovie.getTitleField());
        }

        if (newMovie.getAlsoKnownAsField() != null) {
            alsoKnownAsField.sendKeys(newMovie.getAlsoKnownAsField());
        }

        if (newMovie.getYearField() != null) {
            yearField.sendKeys(newMovie.getYearField());
        }

        if (newMovie.getDurationField() != null) {
            durationField.sendKeys(newMovie.getDurationField());
        }

        if (newMovie.getRatingField() != null) {
            ratingField.sendKeys(newMovie.getRatingField());
        }

        if (newMovie.getFormatField() != null) {
            formatField.clear();
            formatField.sendKeys(newMovie.getFormatField());
        }

        if (newMovie.isOwnThisMovieField() != null) {
            if (newMovie.isOwnThisMovieField()) {
                ownThisMovieField.click();
            } else {
                notOwnThisMovieField.click();
            }
        }

        if (newMovie.isSeenThisMovieField() != null) {
            if (newMovie.isSeenThisMovieField()) {
                seenThisMovieField.click();
            } else {
                notSeenThisMovieField.click();
            }
        }

        if (newMovie.isLoanedThisMovieField() != null) {
            if (newMovie.isLoanedThisMovieField()) {
                loanedThisMovieField.click();
            } else {
                notLoanedThisMovieField.click();
            }
        }

        if (loanName.isDisplayed() && newMovie.getLoanedOutToField() != null) {
            loanName.sendKeys(newMovie.getLoanedOutToField());
        }

        if (loanDate.isDisplayed() && newMovie.getLoanedOutSince() != null) {
            loanDate.clear();
            loanDate.sendKeys(newMovie.getLoanedOutSince());
        }

        if (newMovie.getCoverField() != null) {
            coverField.sendKeys(newMovie.getCoverField());
        }

        if (newMovie.getTrailerUrlField() != null) {
            trailerUrlField.sendKeys(newMovie.getTrailerUrlField());
        }

        if (newMovie.getPersonalNotesField() != null) {
            personalNotesField.sendKeys(newMovie.getPersonalNotesField());
        }

        if (newMovie.getPersonalNotesField() != null) {
            personalNotesField.sendKeys(newMovie.getPersonalNotesField());
        }

        if (newMovie.getTaglinesField() != null) {
            taglinesField.sendKeys(newMovie.getTaglinesField());
        }

        if (newMovie.getPlotOutlineField() != null) {
            plotOutlineField.sendKeys(newMovie.getPlotOutlineField());
        }

        if (newMovie.getPlotsField() != null) {
            plotsField.sendKeys(newMovie.getPlotsField());
        }

        if (newMovie.getLanguagesField() != null) {
            languagesField.sendKeys(newMovie.getLanguagesField());
        }

        if (newMovie.getSubtitlesField() != null) {
            subtitlesField.sendKeys(newMovie.getSubtitlesField());
        }

        if (newMovie.getAudioField() != null) {
            audioField.sendKeys(newMovie.getAudioField());
        }

        if (newMovie.getVideoField() != null) {
            videoField.sendKeys(newMovie.getVideoField());
        }

        if (newMovie.getCountryField() != null) {
            countryField.sendKeys(newMovie.getCountryField());
        }

        if (newMovie.getGenresField() != null) {
            genresField.sendKeys(newMovie.getGenresField());
        }

        if (newMovie.getDirectorField() != null) {
            directorField.sendKeys(newMovie.getDirectorField());
        }

        if (newMovie.getWriterField() != null) {
            writerField.sendKeys(newMovie.getWriterField());
        }

        if (newMovie.getProducerField() != null) {
            producerField.sendKeys(newMovie.getProducerField());
        }

        if (newMovie.getMusicField() != null) {
            musicField.sendKeys(newMovie.getMusicField());
        }

        if (newMovie.getCastField() != null) {
            castField.sendKeys(newMovie.getCastField());
        }
        saveButton.click();

        return MyPageFactory.getPage(driver, MoviePage.class);

    }
}
